//
//  PictureViewController.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/26/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "PictureViewController.h"
#import "TKImageCache.h"
#import "PMGlobal.h"

#define PIC_TAG 100
#define SCROLL_TAG 1000

@interface PictureViewController ()<UIScrollViewDelegate>{
    NSArray *pics_;
    CGRect frame_;
    NSString *desc_;
    
    TKImageCache *imageCache_;
    
    UIScrollView *scrollView_;
}

@end

@implementation PictureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        imageCache_ = [[TKImageCache alloc] initWithCacheDirectoryName:@"image"];
        imageCache_.notificationName = @"picture_detail";
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(picture_detail:) name:@"picture_detail" object:nil];
        
    }
    return self;
}

- (void)dealloc{
    [imageCache_ cancelOperations];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"anniu.png"];
    [b setBackgroundImage:img forState:UIControlStateNormal];
    b.frame = CGRectMake(10, 10, 99, 37);
    [b setTitle:@"返回" forState:UIControlStateNormal];
    [b addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:b];
    self.navigationItem.leftBarButtonItem = right;
    
    self.view.frame = frame_;
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, frame_.size.width, frame_.size.height-64)];
    scroll.pagingEnabled = YES;
    [self.view addSubview:scroll];
    scrollView_ = scroll;
    
    int idx = 0;
    for (NSString *path in pics_) {

        UIScrollView *s = [[UIScrollView alloc] initWithFrame:scroll.bounds];
        s.left = idx * scroll.width;
        s.delegate = self;
        s.tag = SCROLL_TAG + idx;
        s.maximumZoomScale = 2.0;
        s.minimumZoomScale = 1.0;
        [scroll addSubview:s];
        
//        UIImage *img = [UIImage imageWithContentsOfFile:path];
        
        
        
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:scroll.bounds];
        [s addSubview:imgV];
        imgV.tag = PIC_TAG;
        imgV.backgroundColor = [UIColor blackColor];
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        UIImage *img = [imageCache_ imageForKey:[path lastPathComponent] url:[NSURL URLWithString:path] queueIfNeeded:YES tag:idx];
        
        if ([[PMGlobal getInstance]isImageEnable]) {
            if (img) {
            }else{
                img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }
        }else{
            img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
        
        
        imgV.image = img;
        idx++;
    }
    
    scroll.contentSize = CGSizeMake(idx*scroll.width, scroll.height);
    
    if (desc_) {
        UILabel* deslbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 100)];
        deslbl.text = desc_;
        deslbl.bottom = self.view.bottom;
        deslbl.backgroundColor = [UIColor whiteColor];
        deslbl.alpha =0.5;
        deslbl.textAlignment = UITextAlignmentCenter;
        deslbl.font = [UIFont boldSystemFontOfSize:20];
        deslbl.adjustsFontSizeToFitWidth = YES;
        [self.view addSubview:deslbl];
    }
}

- (void)close{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithPictures:(NSArray *)pics withFrame:(CGRect)frame Desc:(NSString*)desc{
    self = [self init];
    if (self) {
        frame_ = frame;
        pics_ = pics;
        desc_ = desc;
    }
    return self;
}

- (int)getCurrentPage:(UIScrollView *)scroll{
    int page = scroll.contentOffset.x/scroll.width;
    return page;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView viewWithTag:PIC_TAG];
}

#pragma mark 显示图片内容
- (void)picture_detail:(NSNotification *)notification{
    
    NSDictionary *dict = [notification userInfo];
    UIImage *img = [dict objectForKey:@"image"];
    int tag = [[dict objectForKey:@"tag"] intValue];
    
    if ([[PMGlobal getInstance]isImageEnable]) {
        if (img) {
        }else{
            img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
    }else{
        img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
    }
    
    
    UIScrollView *s = (UIScrollView *)[scrollView_ viewWithTag:tag+SCROLL_TAG];
    UIImageView *v = (UIImageView *)[s viewWithTag:PIC_TAG];
    v.image = img;
    
}

@end
